package com.sskj.fca.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserBean;
import com.sskj.fca.R;
import com.sskj.fca.http.HttpConfig;
import com.sskj.fca.presenter.AssetActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.APP_ASSET)//我的资产
public class AssetActivity extends BaseActivity<AssetActivityPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private List<String> moneys = new ArrayList<>();
    private SlimAdapter slimAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_asset;
    }

    @Override
    public AssetActivityPresenter getPresenter() {
        return new AssetActivityPresenter();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mPresenter.getQCInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getQCInfo();
    }

    @Override
    protected void initView() {
        setTitle("我的资产");
        smartRefreshLayout.setPrimaryColorsId(R.color.app_title, android.R.color.white);
        smartRefreshLayout.setDragRate(0.3f);//显示下拉高度/手指真实下拉高度=阻尼效果
        smartRefreshLayout.setReboundDuration(300);//回弹动画时长（毫秒）
        smartRefreshLayout.setRefreshHeader(new ClassicsHeader(this));//设置Header
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.getQCInfo();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_asset, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector, List payloads) {
                injector.visibility(R.id.tv_trading_record, View.VISIBLE)
                        .visibility(R.id.btn_recharge, View.VISIBLE)
                        .visibility(R.id.btn_with_draw, View.VISIBLE)
                        .image(R.id.iv_image_type, App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_usdt))
                        .text(R.id.tv_money_type, "USDT")
                        .text(R.id.tv_money, NumberUtil.keep4(data))
                        .clicked(R.id.tv_bill_record, view -> ARouter.getInstance().build(RConfig.APP_BILLIND_RECORD).withString(Constans.MONEY_TYPE, "USDT").navigation())
                        .clicked(R.id.tv_trading_record, view -> ARouter.getInstance().build(RConfig.APP_ASSET_RECORD).withString(Constans.MONEY_TYPE, "USDT").navigation())
                        .clicked(R.id.btn_recharge, view -> ARouter.getInstance().build(RConfig.APP_RECHARGE).withString(Constans.MONEY_TYPE, "USDT").navigation())
                        .clicked(R.id.btn_with_draw, view -> ARouter.getInstance().build(RConfig.APP_WITHDRAW).withString(Constans.MONEY_TYPE, "USDT").navigation());
//                if (slimAdapter.getData().indexOf(data) == 0) {
//                    injector.visibility(R.id.tv_trading_record, View.GONE)
//                            .image(R.id.iv_image_type, App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_qc))
//                            .visibility(R.id.btn_recharge, View.GONE)
//                            .visibility(R.id.btn_with_draw, View.GONE)
//                            .text(R.id.tv_money_type, "JC")
//                            .text(R.id.tv_money, NumberUtil.keep4(data))
//                            .clicked(R.id.tv_bill_record, view -> ARouter.getInstance().build(RConfig.APP_BILLIND_RECORD).withString(Constans.MONEY_TYPE, "JC").navigation());
//                } else {
//                }
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
    }

    public void updateQC(HttpData httpData, UserBean userBean) {
        smartRefreshLayout.finishRefresh();
        if (httpData.getStatus() == HttpConfig.OK) {
            moneys.clear();
//            moneys.add(userBean.getBalance_pb());
            moneys.add(userBean.getBalance_cny());
            slimAdapter.updateData(moneys);
        } else {
            ToastUtil.showShort(httpData.msg);
        }
    }
}
